package com.yalemang.photovalut

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import com.yalemang.photovalut.base.databinding.DialogCenterFragmentBinding
import com.yalemang.photovalut.base.ext.gone
import com.yalemang.photovalut.base.ext.visible
import com.yalemang.photovalut.base.helper.CommonDrawableHelper
import com.yalemang.photovalut.res.R

class CommonCenterDialog : DialogFragment() {

    private var skinColor: Int = Color.RED
    private var title: String = "title"
    private  var isEdit = false
    private var content = ""
    private var cancelClickListener : View.OnClickListener? = null
    private var confirmClickListener : ((String?)->Boolean)? = null
    lateinit var mBinding: DialogCenterFragmentBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = DialogCenterFragmentBinding.inflate(inflater)
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        initView()
        return mBinding.root
    }

    private fun initView() {
        initColor()
        if (isEdit){
            mBinding.etContent.visible()
            mBinding.tvContent.gone()
            mBinding.etContent.setText(content)
            mBinding.etContent.setTextColor(skinColor)
        }else{
            mBinding.etContent.gone()
            mBinding.tvContent.visible()
            mBinding.tvContent.text = content
        }

        mBinding.tvCancel.setOnClickListener{
            cancelClickListener?.onClick(mBinding.tvCancel)
            dismiss()
        }

        mBinding.tvConfirm.setOnClickListener {
            val isDismiss = if (isEdit) {
                confirmClickListener?.invoke(mBinding.etContent.text.toString()) == true
            }else{
                confirmClickListener?.invoke(null) == true
            }

            if (isDismiss){
                dismiss()
            }
        }
    }

    @SuppressLint("ResourceAsColor", "NewApi")
    private fun initColor() {
        mBinding.rl.background = CommonDrawableHelper.Builder().setAll(8f).setColor(Color.WHITE).builder()
        mBinding.tvTitle.background = CommonDrawableHelper.Builder().setTop(8f).setColor(skinColor).builder()
        mBinding.tvTitle.setTextColor(Color.WHITE)
        mBinding.tvConfirm.setTextColor(skinColor)
        mBinding.tvCancel.setTextColor(skinColor)
    }

    fun show(fragmentActivity: FragmentActivity){
        show(fragmentActivity.supportFragmentManager,"")
    }

    class Builder {
        private var skinColor: Int = Color.RED
        private var title: String = "title"
        private var isEdit = false
        private var content = ""
        private var cancelClickListener : View.OnClickListener? = null
        private var confirmClickListener : ((String?)->Boolean)? = null

        fun setSkinColor(skinColor: Int) : Builder {
            this.skinColor = skinColor
            return this
        }

        fun setTitle(title: String) : Builder {
            this.title = title
            return this
        }

        fun setContent(content: String) : Builder{
            this.content = content
            return this
        }

        fun setIsEdit(isEdit: Boolean): Builder{
            this.isEdit = isEdit
            return this
        }

        fun setCancelClick(listener : (View)-> Unit): Builder {
            cancelClickListener = View.OnClickListener {
                listener.invoke(it)
            }
            return this
        }

        fun setConfirmClick(listener : ((String?)->Boolean)): Builder {
            confirmClickListener = listener
            return this
        }

        fun build() : CommonCenterDialog {
            val commonCenterDialog = CommonCenterDialog()
            commonCenterDialog.skinColor = this.skinColor
            commonCenterDialog.title = this.title
            commonCenterDialog.isEdit = this.isEdit
            commonCenterDialog.content = this.content
            commonCenterDialog.cancelClickListener = this.cancelClickListener
            commonCenterDialog.confirmClickListener = this.confirmClickListener
            return commonCenterDialog
        }

    }

}